package cn.fr4nk;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class GovTest {
    private ScriptEngine scriptEngine;

    public GovTest() {
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        this.scriptEngine = scriptEngineManager.getEngineByName("JavaScript");
    }

    public String challenge(String resp){
        resp = resp.substring(8);
        String tmp[] = resp.split("</script");
        resp = tmp[0];
        resp = resp.replace("eval(y.replace", "var aaa=(y.replace");
//        resp = resp + "aaa=aaa.replace(\"h=document.createElement('div');\",\"\");aaa=aaa.replace(\"h.innerHTML='<a href=\\\'/\\\'\",\"\");\n" +
//                "aaa=aaa.replace(\">x</a>';\",\"\");aaa=aaa.replace(\"h=h.firstChild.href;\",\"h='http://www.gsxt.gov.cn/';\");aaa=aaa.replace(\"while(window._phantom||window.__phantomas){};\",\"\");bbb=aaa.split(\"setTimeout\");\n" +
//                "    aaa=bbb[0]+\"return dc;}\";\n" +
//                "    aaa=aaa.replace(\"var l=\",\"{fa:\");\n" +
//                "  var ffa=eval(\"(\"+aaa+\")\");\n" +
//                "    var fffa=ffa.fa();";


        System.out.println(resp);
        String script = resp;
        try {
            scriptEngine.eval(script);
        } catch (ScriptException e) {
            System.out.println(e.getMessage()); 
            return null;
        }
//        String bbb = (String) scriptEngine.get("fffa");
        String bbb = (String) scriptEngine.get("aaa");
        System.out.println(bbb);
        return bbb;
    }
    public String getImageGif(String resp){
        String script="function dd(){var json="+resp+";return json.map( function(item){ return String.fromCharCode(item);}).join('');}" +
                "var ggg=dd();";

        try {
            scriptEngine.eval(script);
        } catch (ScriptException e) {
            return e.getMessage();
        }
        String bbb = (String) scriptEngine.get("ggg");
        return bbb;
    }
    public static void main(String[] s){
    	String rsp = FileUtil.read("C:\\Users\\fan.yang\\eclipse-workspace\\jiyan-hack\\o");
    	new GovTest().challenge(rsp);
    }
}
